#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class StudentRoomProvider : CY.GFive.Core.Providers.DALProvider.IStudentRoomProvider
	{
		#region Sql
		private static readonly string SqlSelectStudentRoom = "SELECT [Id], [StudentCode],[RoomCode],[StayStartDate],[StayEndDate],[IsState] FROM [StudentRoom] WHERE  [Id]=@Id";

		private static readonly string SqlInsertStudentRoom = "INSERT INTO [StudentRoom]([StudentCode],[RoomCode],[StayStartDate],[StayEndDate],[IsState]) values(@StudentCode,@RoomCode,@StayStartDate,@StayEndDate,@IsState);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateStudentRoom = "UPDATE [StudentRoom] SET [StudentCode] = @StudentCode,[RoomCode] = @RoomCode,[StayStartDate] = @StayStartDate,[StayEndDate] = @StayEndDate,[IsState] = @IsState WHERE  [Id]=@Id";

		private static readonly string SqlDeleteStudentRoom = "DELETE [StudentRoom] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllStudentRoom = "SELECT [Id], [StudentCode],[RoomCode],[StayStartDate],[StayEndDate],[IsState] FROM [StudentRoom]";
		#endregion

		#region IStudentRoomProvider Members
		public Core.Business.StudentRoom Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectStudentRoom);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.StudentRoom studentRoom = new Core.Business.StudentRoom();

				if (!reader.IsDBNull(0)) studentRoom.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) studentRoom.StudentCode = reader.GetString(1);
				if (!reader.IsDBNull(2)) studentRoom.RoomCode = reader.GetString(2);
				if (!reader.IsDBNull(3)) studentRoom.StayStartDate = reader.GetDateTime(3);
				if (!reader.IsDBNull(4)) studentRoom.StayEndDate = reader.GetDateTime(4);
				if (!reader.IsDBNull(5)) studentRoom.IsState = reader.GetBoolean(5);

				return studentRoom;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.StudentRoom studentRoom)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@StudentCode", SqlDbType.NVarChar, studentRoom.StudentCode);
			sql.AddParameter("@RoomCode", SqlDbType.NVarChar, studentRoom.RoomCode);
			sql.AddParameter("@StayStartDate", SqlDbType.DateTime, studentRoom.StayStartDate);
			sql.AddParameter("@StayEndDate", SqlDbType.DateTime, studentRoom.StayEndDate);
			sql.AddParameter("@IsState", SqlDbType.Bit, studentRoom.IsState);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertStudentRoom);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) studentRoom.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.StudentRoom studentRoom)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@StudentCode", SqlDbType.NVarChar, studentRoom.StudentCode);
			sql.AddParameter("@RoomCode", SqlDbType.NVarChar, studentRoom.RoomCode);
			sql.AddParameter("@StayStartDate", SqlDbType.DateTime, studentRoom.StayStartDate);
			sql.AddParameter("@StayEndDate", SqlDbType.DateTime, studentRoom.StayEndDate);
			sql.AddParameter("@IsState", SqlDbType.Bit, studentRoom.IsState);
			sql.AddParameter("@Id", SqlDbType.Int, studentRoom.Id);
			sql.ExecuteSql(SqlUpdateStudentRoom);
		}

		public void Delete(Core.Business.StudentRoom studentRoom)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, studentRoom.Id);
			sql.ExecuteSql(SqlDeleteStudentRoom);
		}

		public IList<Core.Business.StudentRoom> GetAllStudentRoom()
		{
			IList<Core.Business.StudentRoom> studentRoomlist = new List<Core.Business.StudentRoom>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllStudentRoom);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.StudentRoom studentRoom = new Core.Business.StudentRoom();

					if (!reader.IsDBNull(0)) studentRoom.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) studentRoom.StudentCode = reader.GetString(1);
					if (!reader.IsDBNull(2)) studentRoom.RoomCode = reader.GetString(2);
					if (!reader.IsDBNull(3)) studentRoom.StayStartDate = reader.GetDateTime(3);
					if (!reader.IsDBNull(4)) studentRoom.StayEndDate = reader.GetDateTime(4);
					if (!reader.IsDBNull(5)) studentRoom.IsState = reader.GetBoolean(5);

					studentRoom.MarkOld();
					studentRoomlist.Add(studentRoom);
				}
				reader.Close();
			}
			return studentRoomlist;
		}
		#endregion
	}
}
